﻿using Newtonsoft.Json;
using QuickShare.Vsix.SqlList.Commons;
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace QuickShare.Vsix.SqlList.Forms
{
    public partial class FrmDeployInfos : Form
    {
        private readonly string configFile;
        private static string pattern = "^(.[a-z0-9]{1,6};)*(.[a-z0-9]{1,6})?$";
        public DeployInfo Value { get; private set; }
        public FrmDeployInfos(string projectName, string configFile, string message) : this()
        {
            this.Text = $"配置 {projectName} 发布目录";
            label2.Text = message;
            label8.Text = null;
            this.txtExts.Text = ".html;.js;.css";
            if(File.Exists(configFile))
            {
                var text = File.ReadAllText(configFile);
                if(!string.IsNullOrWhiteSpace(text))
                {
                    var dp = Helper.TryParseFromJson<DeployInfo>(text);
                    if(dp != null && !string.IsNullOrWhiteSpace(dp.Path))
                    {
                        txtPath.Text = dp.Path;
                        txtExts.Text = dp.StaticFileExts;
                        txtRoot.Text = dp.ProjectRootPath;
                    }
                }
            }
            this.configFile = configFile;
        }
        public FrmDeployInfos()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(txtPath.Text))
            {
                label2.Text = "配置的发布目录不正确";
                return;
            }
            var path = txtPath.Text.Trim();
            if(!Directory.Exists(path))
            {
                label2.Text = "配置的发布目录不存在";
                return;
            }
            label2.Text = null;
            var exts = txtExts.Text.Trim();
            if(!Regex.IsMatch(exts, pattern, RegexOptions.IgnoreCase))
            {
                label8.Text = "配置的扩展名格式不正确";
                return;
            }
            label8.Text = null;
            Value = new DeployInfo
            {
                Path = path,
                StaticFileExts = txtExts.Text.Trim(),
                ProjectRootPath = txtRoot.Text.Trim(),
            };

            var targetPath = Path.GetDirectoryName(configFile);
            Helper.TryCreateDir(targetPath);

            var text = JsonConvert.SerializeObject(Value, Formatting.Indented);
            File.WriteAllText(configFile, text);
            this.DialogResult = DialogResult.OK;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            var opd = new FolderBrowserDialog
            {
                Description = "请选择已经映射到TFS服务器路径的目录\r\n该目录为脚本目录(SqlScripts)的父级目录"
            };
            var path = txtPath.Text.Trim();
            if(Directory.Exists(path))
            {
                opd.SelectedPath = path;
            }
            if(opd.ShowDialog() == DialogResult.OK)
            {
                this.txtPath.Text = opd.SelectedPath;
            }
        }
    }
}
